package com.yaoandw.crawl.storage;

import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.yaoandw.crawl.model.CrawlUrl;
import com.yaoandw.crawl.storage.dao.DaoUtil;
import com.yaoandw.lucene.Page;
import com.yaoandw.util.URLUtil;

public class Storage {
	private static Storage instance;
//	private static String[] binaryFileTypes= new String[]{".jpg",".png",".jpeg",".ico",".bmp",".gif",".zip",".rar",".gz",".tar"};
	private static List<String> binaryFileTypes= Arrays.asList(new String[]{".jpg",".png",".jpeg",".ico",".bmp",".gif",".zip",".rar",".gz",".tar",".js",".js?5YW",".css?5YW",".css"});
	private static List<String> ignorePathStrings = Arrays.asList(new String[]{"ac=sendmail"});
	
	private Set<CrawlUrl> unCrawledUrl = new HashSet<CrawlUrl>();
//	private Set<CrawlUrl> crawledUrl = new HashSet<CrawlUrl>();
	private Set<URL> crawledUrl = new HashSet<URL>();
	public Set<URL> getCrawledUrl() {
		return crawledUrl;
	}
	private boolean baseUrlOnly;
	private URL baseUrl;
	private int level;
	private Storage(URL baseUrl,boolean baseUrlOnly,int level) {
		this.baseUrl = baseUrl;
		this.baseUrlOnly = baseUrlOnly;
		this.level = level;
	}
	public static Storage getInstance(URL baseUrl,boolean baseUrlOnly,int level) {
		if(instance != null)
			return instance;
		instance = new Storage(baseUrl,baseUrlOnly,level);
		DaoUtil.getPageDao().createTableIfNotExists();
		return instance;
	}
	public boolean hasNext() {
		return !unCrawledUrl.isEmpty();
	}
	public CrawlUrl getNextUrl() {
		CrawlUrl crawlUrl = unCrawledUrl.iterator().next();
		crawledUrl.add(crawlUrl.getUrl());
		unCrawledUrl.remove(crawlUrl);
		return crawlUrl;
	}
	public boolean put(URL foundURL,int level) {
//		if(foundURL.toString().indexOf("=forumdisplay&fid=55")!= -1){
//			System.out.println();
//		}
		if(this.level > 0 && level > this.level)
			return false;
		if(baseUrlOnly && !foundURL.getHost().equals(baseUrl.getHost())){
			return false;
		}
		for(String str : ignorePathStrings){
			String fullpath = foundURL.toString();
			if(fullpath.indexOf(str) != -1)
				return false;
		}
		if(URLUtil.isFileSpecified(foundURL)){
			String path = foundURL.getPath();
			int posLastDot = path.lastIndexOf('.');
			String suffix = path.substring(posLastDot);
			if(binaryFileTypes.contains(suffix))
				return false;
		}
		if(crawledUrl.contains(foundURL))
			return false;
		CrawlUrl newCrawlUrl = new CrawlUrl(foundURL,level);
		if(unCrawledUrl.contains(newCrawlUrl))
			return false;
		unCrawledUrl.add(newCrawlUrl);
		return true;
	}
	public boolean pageHasModified(Page page) {
		Page p = DaoUtil.getPageDao().getPage(page.getUrl());
		if(p == null){
			DaoUtil.getPageDao().addPage(page);
		}else{
			if(!p.getPageHash().equals(page.getPageHash()))
				DaoUtil.getPageDao().updatePage(page);
			else
				return false;
		}
		return true;
	}
	public void deletePage(String url) {
		DaoUtil.getPageDao().deletePage(url);
	}
}
